﻿/** \file
* \$Rev: 184 $
* 
* \$Date: 2010-04-14 07:18:41 +0000 (Wed, 14 Apr 2010) $
*
* \$URL: http://chorus-twitter.googlecode.com/svn/trunk/src/utils/Utils.cs $
*/

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace chorus.utils
{
	/// <summary>
	/// A generic utility class.
	/// </summary>
	public static class Utils
	{
		/// <summary>Invoke a method on a control.</summary>
		/// <param name="control">The control whose thread will be used.</param>
		/// <param name="method">The method to invoke.</param>
		public static void InvokeControl(Control control, MethodInvoker method)
		{
			if (control.InvokeRequired)
				control.Invoke(method);
			else
				method();
		}
	}

	/// <summary>
	/// A BindingList that disposes items that are removed.
	/// </summary>
	/// <typeparam name="ElmType">The item type.</typeparam>
	public class DisposableBindingList<ElmType> :
		BindingList<ElmType>
		where ElmType : IDisposable
	{
		/// <summary>Remove an item.</summary>
		/// <param name="index">The index of the item to remove.</param>
		protected override void RemoveItem(int index)
		{
			this[index].Dispose();
			base.RemoveItem(index);
		}
	}
}
